﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class LoaiDealDAO
    {
        private FDeal4Entities db;

        public LoaiDealDAO()
        {
            db = new FDeal4Entities();
        }

        public List<LOAIDEAL> LietKe()
        {
            return db.LOAIDEAL.ToList();
        }

        public LOAIDEAL LayID(string id)
        {
            var tl = db.LOAIDEAL.SingleOrDefault(t => t.IDHINHTHUC == id);
            return tl;
        }

        public LOAIDEAL TimTen(String keyWord)
        {
            var tl = db.LOAIDEAL.SingleOrDefault(t => t.TENLOAI.Contains(keyWord));
            return tl;
        }

        public bool Them(LOAIDEAL tl)
        {
            try
            {
                db.LOAIDEAL.AddObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Xoa(LOAIDEAL tl)
        {
            try
            {
                db.LOAIDEAL.DeleteObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Sua(LOAIDEAL tl)
        {
            try
            {
                LOAIDEAL t1 = db.LOAIDEAL.Single(t => t.IDHINHTHUC == tl.IDHINHTHUC);
                t1.IDLOAIDEAL = tl.IDLOAIDEAL;
                t1.IDHINHTHUC = tl.IDHINHTHUC;
                t1.TENLOAI = tl.TENLOAI;

                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
            	return false;
            }
        }
    }
}
